import { apiClient } from "./index";

export interface RechargePackage {
  id: string;
  amount: number;
  price: number;
  bonus: number;
  description: string;
  isPopular?: boolean;
}

export interface VipPackage {
  type: "monthly" | "yearly";
  name: string;
  price: number;
  duration: string;
  features: string[];
}

export interface PaymentOrder {
  orderNo: string;
  amount: number;
  status: "pending" | "paid" | "failed" | "cancelled";
  paymentMethod: string;
  createdAt: string;
}

export interface TransactionRecord {
  id: string;
  type: "recharge" | "consumption" | "vip";
  amount: number;
  description: string;
  createdAt: string;
  status: string;
}

// 获取充值套餐
export const getRechargePackages = async (): Promise<RechargePackage[]> => {
  const response = await apiClient.get("/payment/packages");
  return (response.data as RechargePackage[]) || [];
};

// 创建充值订单
export const createRechargeOrder = async (data: {
  packageId: string;
  paymentMethod: string;
}): Promise<PaymentOrder> => {
  const response = await apiClient.post("/payment/recharge", data);
  return response.data as PaymentOrder;
};

// 获取VIP套餐
export const getVipPackages = async (): Promise<VipPackage[]> => {
  const response = await apiClient.get("/payment/vip-packages");
  return (response.data as VipPackage[]) || [];
};

// 购买VIP
export const buyVip = async (data: {
  type: "monthly" | "yearly";
  paymentMethod: string;
}): Promise<PaymentOrder> => {
  const response = await apiClient.post("/payment/vip", data);
  return response.data as PaymentOrder;
};

// 检查订单状态
export const checkOrderStatus = async (orderNo: string): Promise<PaymentOrder> => {
  const response = await apiClient.get(`/payment/order/${orderNo}`);
  return response.data as PaymentOrder;
};

// 获取交易记录
export const getTransactionRecords = async (params?: {
  page?: number;
  limit?: number;
  type?: string;
}): Promise<{
  records: TransactionRecord[];
  total: number;
  page: number;
  limit: number;
}> => {
  const queryParams = new URLSearchParams();
  if (params?.page) queryParams.append("page", params.page.toString());
  if (params?.limit) queryParams.append("limit", params.limit.toString());
  if (params?.type) queryParams.append("type", params.type);

  const queryString = queryParams.toString();
  const endpoint = queryString ? `/payment/records?${queryString}` : "/payment/records";

  const response = await apiClient.get(endpoint);
  return (
    (response.data as {
      records: TransactionRecord[];
      total: number;
      page: number;
      limit: number;
    }) || { records: [], total: 0, page: 1, limit: 10 }
  );
};

// 模拟支付成功（开发用）
export const mockPaymentSuccess = async (
  orderNo: string,
): Promise<{ success: boolean; message: string }> => {
  const response = await apiClient.post(`/payment/mock-success/${orderNo}`);
  return response;
};

// 获取用户钱包信息
export const getWalletInfo = async (): Promise<{
  balance: number;
  totalRecharge: number;
  totalConsumption: number;
  vipInfo?: {
    isVip: boolean;
    vipType?: string;
    expiresAt?: string;
  };
}> => {
  const response = await apiClient.get("/payment/wallet");
  return (
    (response.data as {
      balance: number;
      totalRecharge: number;
      totalConsumption: number;
      vipInfo?: {
        isVip: boolean;
        vipType?: string;
        expiresAt?: string;
      };
    }) || { balance: 0, totalRecharge: 0, totalConsumption: 0 }
  );
};
